import { _decorator, Component, Node, tween, Tween } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('AutoSwingAnim')
export class AutoSwingAnim extends Component {

    @property
    offsetAngle = 0;
    @property
    stepAngle = 30;

    @property
    animTime = 2;

    curt = 0;
    animData = { angle: 0 }

    protected onEnable(): void {
        this.curt = 0;
        this.animData.angle = -this.stepAngle * 0.5;
        this.node.angle = this.offsetAngle + this.animData.angle;

        Tween.stopAllByTarget(this.animData);

        tween(this.animData).repeatForever(
            tween(this.animData).
                to(this.animTime * 0.5, { angle: this.stepAngle * 0.5 }, { easing: 'sineInOut' }).
                to(this.animTime * 0.5, { angle: -this.stepAngle * 0.5 }, { easing: 'sineInOut' })
        ).start();

    }

    protected onDisable(): void {
        Tween.stopAllByTarget(this.animData);
    }

    update(dt: number) {
        this.node.angle = this.offsetAngle + this.animData.angle;
    }
}

